/**
 * 版权所有 (C), 2019-2019, XXX有限公司
 * 项目名：com.company.study.dsa.queue
 * 文件名: MyQueue
 * 日期: 2019/6/17 21:25
 * 说明:
 */
package com.company.study.dsa.queue;

/**
 * 单向队列
 *
 * @author Anbang713
 * @create 2019/6/17
 */
public class MyQueue {
    private Object[] array;
    //队列总大小
    private int maxSize;
    //前端
    private int front;
    //后端
    private int rear;
    //队列中元素的实际数目
    private int itemSize;

    public MyQueue(int maxSize) {
        this.maxSize = maxSize;
        array = new Object[maxSize];
        front = 0;
        rear = -1;
        itemSize = 0;
    }

    public boolean isFull() {
        return itemSize == maxSize;
    }

    public boolean isEmpty() {
        return itemSize == 0;
    }

    public int getSize() {
        return itemSize;
    }

    /**
     * 返回对头元素
     *
     * @return
     */
    public Object peekFront() {
        return array[front];
    }

    /**
     * 向队列中插入数据
     *
     * @param value
     */
    public void insert(Object value) {
        if (isFull()) {
            System.out.println("队列已满！！！");
            return;
        }
        //如果队列尾部指向顶了，那么循环回来，执行队列的第一个元素
        if (rear == maxSize - 1) {
            rear = -1;
        }
        //队尾指针加1，然后在队尾指针处插入新的数据
        array[++rear] = value;
        itemSize++;
    }

    /**
     * 移除队头数据
     *
     * @return
     */
    public Object remove() {
        Object removeValue = null;
        if (isEmpty() == false) {
            removeValue = array[front];
            array[front++] = null;
            if (front == maxSize) {
                front = 0;
            }
            itemSize--;
        }
        return removeValue;
    }
}